package com.vdong.interactive.course.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.vdong.interactive.course.common.exception.ProcessException;
import com.vdong.interactive.course.entity.dto.CourseDetailDTO;
import com.vdong.interactive.course.entity.dto.PeddleChoiceCourseListDTO;
import com.vdong.interactive.course.service.CourseService;
import com.vdong.kpay.basics.dubbo.api.backend.CourseMainService;
import com.vdong.kpay.basics.dubbo.api.backend.StoreService;
import com.vdong.kpay.basics.dubbo.api.frontend.AppCourseService;
import com.vdong.kpay.basics.dubbo.pojo.backend.CourseMain;
import com.vdong.kpay.basics.dubbo.pojo.backend.StoreInfo;
import com.vdong.kpay.basics.dubbo.pojo.backend.UserMain;
import com.vdong.kpay.basics.dubbo.pojo.frontend.AppCourseSub;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

//@Service
@org.springframework.stereotype.Service
public class CourseServiceImpl implements CourseService {

    private static final String PREFIX = "http://";

    @Reference(version = "1.0.0")
    private CourseMainService courseMainService;

    @Reference
    private AppCourseService appCourseService;

    @Reference(version = "1.0.0")
    private StoreService storeService;

    /**
     * 分销选择分销商品(商户)
     */
    @Override
    public PageInfo<PeddleChoiceCourseListDTO> getGoodsChoicePage(Long userId,Long stroeId,String courseName,Integer page,Integer pageSize) {
        PageInfo<CourseMain> courseMainPageInfo = courseMainService.getPriceCourses(stroeId, courseName, page, pageSize);
        List<PeddleChoiceCourseListDTO> courseListDTOS = new ArrayList<>();
        courseMainPageInfo.getList().forEach(e -> {
            PeddleChoiceCourseListDTO courseListDTO = new PeddleChoiceCourseListDTO();
            courseListDTO.setCourseId(e.getId());//课程id
            courseListDTO.setCourseName(e.getCourseName());//课程名称
            courseListDTO.setClassifyType(0);//分类类型 目前只有 0 ：课程
            courseListDTOS.add(courseListDTO);
        });

        PageInfo pageInfo = new PageInfo(courseListDTOS);
        BeanUtils.copyProperties(courseMainPageInfo,pageInfo,"list");
        return pageInfo;
    }

    /**
     * 课程详情
     * @param courseId
     * @return
     */
    @Override
    public CourseDetailDTO getCourseDetail(Long courseId,Long storeId) {
        CourseDetailDTO courseDetailDTO = new CourseDetailDTO();
        StoreInfo storeInfo = storeService.getStoreInfo(storeId,null);
        UserMain userMain = storeInfo.getUserMain();

        if (userMain.getVersionLevel().compareTo(2) == 0){
            courseDetailDTO.setPlatformRate(20);
        }else if (userMain.getVersionLevel().compareTo(3) == 0){
            courseDetailDTO.setPlatformRate(15);
        }else if (userMain.getVersionLevel().compareTo(4) == 0){
            courseDetailDTO.setPlatformRate(10);
        }else if (userMain.getVersionLevel().compareTo(5) == 0){
            courseDetailDTO.setPlatformRate(5);
        }

        courseDetailDTO.setVersionLevel(userMain.getVersionLevel());
        CourseMain courseMain = courseMainService.getDubboCourseMainInfo(courseId);
        courseDetailDTO.setCourseId(courseMain.getId());
        courseDetailDTO.setCourseIntroduction(courseMain.getIntroduction());
        courseDetailDTO.setCourseName(courseMain.getCourseName());
        courseDetailDTO.setGoodsType(0);
        courseDetailDTO.setImageUrl(PREFIX + courseMain.getStoreDomain() + "/" + courseMain.getCourseUrlFirst());
        courseDetailDTO.setAuthorId(courseMain.getUserInfo().getId());
        courseDetailDTO.setAuthorName(courseMain.getUserInfo().getContact());
        List<AppCourseSub> subList = appCourseService.listSubDubbo(courseId,false);

        if (subList != null && subList.size() > 0){
            courseDetailDTO.setSubType(subList.get(0).getSubType().toString());
        }
        return courseDetailDTO;
    }
}
